#include "head.h"


void *thread_main(void *p){
    thread_pool_t *tp = (thread_pool_t*)p;

    while(1){
        //线程获得锁准备工作
        int ret = pthread_mutex_lock(&(tp->lock));
        ERROR_CHECK(ret, -1, "pthread_mutex_lock_threadMain.c");

        //当任务队列中没有任务时，线程挂起等待
        while(tp->queue.queue_size <= 0){
            ret = pthread_cond_wait(&(tp->cond), &(tp->lock));
            ERROR_CHECK(ret, -1, "pthread_cond_wait_threadMain.c");
        }

        //任务队列中有任务了
        int netFD = tp->queue.head->netFD;
        //出队列
        dequeue(&(tp->queue));

        //释放锁，让给其他等待工作的线程
        ret = pthread_mutex_unlock(&(tp->lock));

        printf("线程%lu开始工作\n", pthread_self());
        //线程获得任务开始工作
        start_work(netFD);
        printf("线程%lu结束工作\n", pthread_self());

    }
}



